package timing.ukulele.item.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import timing.ukulele.item.service.ItemService;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.ResultCode;
import timing.ukulele.item.client.ProjectClient;
import timing.ukulele.item.persistent.Item;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("/item")
@Api("事项接口")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Resource
    private ProjectClient projectClient;

    @GetMapping("/page")
    @ApiOperation("分页获取事项列表")
    public ResponseResult<IPage<Item>> page(
            @ApiParam(value = "查询名称", required = true) @RequestParam String name,
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
            @ApiParam(value = "系统权限ID") @RequestHeader(Constant.SYSTEM) String systemIds) {
        // String systemIds = "1, 1326390830101028866, 1326391202156765185, 1326391346805727234, 1326391436677079042, 1326391548857933826, 1326391642428661761, 1326391804022611969, 1326391891947806721, 1326398858841829378, 1326807910528876546, 1328902971995566081, 1330761896646705153, 1331786725621538817, 1331798987254050817, 1331864533060300802, 1341980290758352898, 1422488072509575170";
        return ResponseResult.success(itemService.getPage(name, systemIds, pageNum, pageSize, new HashMap<>()));
    }

    @PostMapping("/create")
    @ApiOperation("保存事项")
    @ApiImplicitParam(name = "item", value = "事项对象", required = true, dataType = "Item")
    public ResponseResult<Item> create(
            @ApiParam(value = "事项对象", required = true) @RequestBody Item item,
            @RequestHeader(Constant.USERNAME) String username) {
        item.setCreateBy(username);
        itemService.save(item);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改事项信息")
    @ApiImplicitParam(name = "item", value = "事项对象", required = true, dataType = "Item")
    public ResponseResult<Item> update(@ApiParam("用户对象") @RequestBody Item item,
                                       @RequestHeader(Constant.USERNAME) String username) {
        // ){
        item.setCreateBy(username);
        itemService.updateById(item);
        return ResponseResult.success();
    }

    @PostMapping("/validateItemCode")
    @ApiOperation("校验事项编码是否已存在")
    public ResponseResult<Boolean> validateItemCode(
            @ApiParam("事项编码") @RequestParam String itemCode,
            @ApiParam(value = "更新事项的ID", example = "0") @RequestParam Long updateItemId) {
        Item item = itemService.getByCode(itemCode);
        if (item != null) {
            if (item.getId().equals(updateItemId)) {
                return ResponseResult.success(true);
            }
            return ResponseResult.success(false);
        }
        return ResponseResult.success(true);
    }

    @PostMapping("/saveItem")
    @ApiOperation("保存事项")
    @ApiImplicitParam(name = "item", value = "事项对象", required = true, dataType = "Item")
    public ResponseResult<Item> saveItem(
            @ApiParam(value = "事项对象", required = true) @RequestBody Item item,
            @RequestHeader(Constant.USERNAME) String username) {
        // ){
        item.setCreateBy(username);
        itemService.saveItem(item);
        return ResponseResult.success();
    }

    @DeleteMapping
    @ApiOperation("删除事项")
    public ResponseResult<String> removeItem(@RequestParam("itemId") Long itemId) {
        ResponseResult<Integer> projectExist = projectClient.checkItemProjectExist(itemId);
        if (projectExist == null) {
            return ResponseResult.error("查询是否该事项是否存在办件失败");
        }
        if (projectExist.getCode() == ResultCode.RPC_ERROR.getCode()) {
            return ResponseResult.error("查询是否该事项是否存在办件失败");
        }
        if (projectExist.getData() == null || projectExist.getData() == 0) {
            itemService.removeById(itemId);
            return ResponseResult.success();
        } else {
            return ResponseResult.error("该事项已产生办件，不能删除");
        }
    }

}
